/*******************************************************
*文件名：screenammager.cpp
*创建人：buptsoft团队
*创建日期：2014/3/18
*修改人：
*日期:
*描述：这是一个用于将tmanager投影到屏幕上指定区域的类，这个类也负责
*     行的划分。
*版本：v0.0.5 buptsoftCopyright(c).
********************************************************/
#ifndef _INC_SCREENMANAGER
#define _INC_SCREENMANAGER
#include "tmanager.cpp"
#include <QApplication>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<QPainter>
#include <conio.h>
#include<QDebug>
#include<QClipboard>
typedef struct linelink{
    Filelink * c;
    struct linelink* last;
    struct linelink* next;
}Linelink;
typedef struct shuqian{
    int n;
    int d;
}Shuqian;
class Screenmanager{
public:
    bool note_show=true;
    char * name;
    char * shortname;
private:
    Tmanager* POINTER_T;
    Filelink * linepointer=NULL;
    Filelink * pre_pointer=NULL;
    Filelink * next_pointer=NULL;
    Linelink * CURLINE=NULL;
    Linelink * headline=NULL;
    Linelink * NOTELINE=NULL;
    Linelink * DOWNLINE=NULL;
    int X_LEFT,X_RIGHT,Y_UP,Y_DOWN,LETTERSIZE,NOTE_X,NOTE_Y;
    bool RES_L_OUT=FALSE,NOTE_IN_SIGHT=FALSE,JUST_INSERTED=FALSE,JUST_DELETED=FALSE;
    QPixmap enter;
    Filelink * res_note=NULL;
public:
    /*******************************************************
    *函数名：Screenmanager()
    *功能:对Screenmanager类中的所有变量初始化，并且根据传入的文件名初
    *    始化tmanager类，并进行直接管理。
    *返回值：无返回值
    *参数：char* string 为打开的文件名
    *备注：该函数在初始化时必须调用
    ********************************************************/
    Screenmanager(char*string,int size=0){
        name=string;
        FILE* fp=fopen(string,"a");
        fclose(fp);
        POINTER_T=new Tmanager(string);
        linepointer=POINTER_T->headF->next;
        X_LEFT=220;
        X_RIGHT=750;
        Y_UP=90;
        Y_DOWN=550;
        if(size)
            LETTERSIZE=size;
        else
            LETTERSIZE=15;
        create_linelinks();
        //POINTER_T->note=POINTER_T->note->next;
        POINTER_T->note=NULL;
        //init_shortname();
        shortname=Get_short_name(name);
        enter.load("image/enter.png");
    }
    void lettersize_up(int s){
        if(LETTERSIZE+s<=150)
            set_lettersize(LETTERSIZE+s);
    }
    void lettersize_down(int s){
        if(LETTERSIZE-s>=10)
            set_lettersize(LETTERSIZE-s);
    }
    void set_lettersize(int l){
        if(l){
            LETTERSIZE=l;
            refresh_linelinks();
        }
    }
    int get_lettersize(){
        return LETTERSIZE;
    }
    char* Get_short_name(char* name)
    {
        int length=16;
        char* short_name = (char*)malloc(sizeof(char)*(length+1));

        int i = 0, j = -1, flage = 0;
        while(name[i] != 0){
            if(name[i] == '/'||name[i] == '\\')
                j = i;
            i++;
        }
        i = j+1;
        j = 0;
        while(name[i] != '\0' && flage == 0){
            if(j <= length - 1){
                if(name[i] > 0){
                    short_name[j++] = name[i++];
                }else if(name[i] < 0){
                            short_name[j++] = name[i++];
                            short_name[j++] = name[i++];
                }
            }
            if(j >= length){
                short_name[j-1] = '.';
                short_name[j-2] = '.';
                flage = 1;
            }
        }
        short_name[j] = 0;
        return short_name;
    }
    Filelink * return_headF(){
        return POINTER_T->headF->next;
    }
    char * return_content(){
        int i=0;
        char * str=(char *)malloc(1024*sizeof(char));
        Filelink * TEMP;
            TEMP=POINTER_T->headF->next;
        for(;i<1024&&TEMP&&TEMP->next;TEMP=TEMP->next){
            str[i++]=TEMP->text[0];
            if(TEMP->text[0]<0){
                str[i++]=TEMP->text[1];
            }
        }
        str[i]=0;
        return str;
    }
    char * return_block(){
        int i=0;
        int length=5;
        Filelink * TEMP;
            TEMP=POINTER_T->return_buffer();
        for(;TEMP&&TEMP!=NULL;TEMP=TEMP->next)
            length+=TEMP->text[0]<0?2:1;
        char * str=(char *)malloc(length*sizeof(char));
        for(TEMP=POINTER_T->return_buffer();i<length&&TEMP&&TEMP!=NULL;TEMP=TEMP->next){
            str[i++]=TEMP->text[0];
            if(TEMP->text[0]<0){
                str[i++]=TEMP->text[1];
            }
        }
        str[i]=0;
        return str;
    }
    int return_number(){
        char* str=return_content();
        int sum=0;
        for(int i=0;str[i]!=0&&'0'<=str[i]&&'9'>=str[i];i++)
            sum*=10,sum+=str[i]-'0';
        if(sum>=10&&sum<=150)
            return sum;
        else \
            return 0;
    }
    int return_curlines(){
        int sum=1;
        for(Linelink* TEMP=headline;TEMP&&TEMP->next&&TEMP!=CURLINE;TEMP=TEMP->next)
            sum++;
        return sum;
    }
    int set_line(int line){
        CURLINE=headline;
        for(int i=0;i<line;i++)
            if(CURLINE&&CURLINE->next)
                CURLINE=CURLINE->next;
    }
    int total_lines(){
        int sum=1;
        for(Linelink* TEMP=headline;TEMP&&TEMP->next;TEMP=TEMP->next)
            sum++;
        return sum;
    }
    bool search(Filelink * f){
        if(f->next&&POINTER_T->search(f)){
            Linelink* TEMPLINE=headline;
            Filelink* TEMPLINK=headline->c;
            for(;TEMPLINE&&TEMPLINK;TEMPLINK=TEMPLINK->next){
                if(TEMPLINK==POINTER_T->res.left)
                    CURLINE=TEMPLINE;
                if(TEMPLINK&&TEMPLINE->next&&TEMPLINK->next==TEMPLINE->next->c)
                    TEMPLINE=TEMPLINE->next;
            }
            return true;
        }
        else
            return false;
    }
    bool inside_region(int x,int y){
        return (X_LEFT<=x&&x<=X_RIGHT&&Y_UP<=y&&y<=Y_DOWN);
    }
    void note_to_head(){
        POINTER_T->note=POINTER_T->headF->next;
    }
    void set_size(int left,int top,int right,int down){
        X_LEFT=left;
        X_RIGHT=right;
        Y_UP=top;
        Y_DOWN=down;
        refresh_linelinks();
    }
    void update(){
        Linelink * BUFFER=CURLINE;
        Filelink *FILELINK=CURLINE->c;
        int X_POS=X_LEFT,Y_POS=Y_UP+LETTERSIZE;
        bool LAST_NOTE_IN_SIGHT=NOTE_IN_SIGHT,FULL=FALSE;
        NOTE_IN_SIGHT=FALSE;
        for(;X_POS<=X_RIGHT-LETTERSIZE && Y_POS<=Y_DOWN &&FILELINK &&FILELINK->next;){
            char ch=FILELINK->text[0];
            if(FILELINK==POINTER_T->note){
                //if(note_show)pp->drawText(QPoint(X_POS+LETTERSIZE/2+2,Y_POS),QString("|"));
                NOTE_X=(X_POS-X_LEFT);
                NOTE_Y=Y_POS;
                NOTELINE=BUFFER;
            }
            else if(POINTER_T->note&&FILELINK->last==POINTER_T->note&&FILELINK!=CURLINE->c||POINTER_T->note&&POINTER_T->note->next&&!POINTER_T->note->next->next)
                NOTE_IN_SIGHT=TRUE;
            if(FILELINK&&FILELINK->text&&FILELINK->text[0]<0)
                X_POS+=LETTERSIZE;
            else if(FILELINK&&FILELINK->text&&FILELINK->text[0]=='\t')
                X_POS+=2*LETTERSIZE;
            else
                X_POS+=LETTERSIZE/2;
            if(BUFFER&& BUFFER->next&&FILELINK->next==BUFFER->next->c){
                X_POS=X_LEFT;
                Y_POS+=LETTERSIZE;
                if(BUFFER&&BUFFER->next){
                    BUFFER=BUFFER->next;
                    FILELINK=BUFFER->c;
                }

            }
            else if(FILELINK->next)
                FILELINK=FILELINK->next;
            else
                break;
            if(Y_POS>Y_DOWN)
                FULL=TRUE;
        }
        next_pointer=FILELINK;
        DOWNLINE=BUFFER;
        if(!(POINTER_T->res.right&&POINTER_T->res.left)){
            POINTER_T->res.right=NULL;
            POINTER_T->res.left=NULL;
        }
        if(POINTER_T->note&&!POINTER_T->note->next&&POINTER_T->note->last)
            POINTER_T->note=POINTER_T->note->last;
        if(FULL&&JUST_INSERTED&&!NOTE_IN_SIGHT)
            rool_down();
        JUST_INSERTED=FALSE;
        JUST_DELETED=FALSE;

    }

    /*******************************************************
    *函数名：print()
    *功能:在屏幕的指定位置打印tmanager中的指定内容，区域由X_LEFT,X_R
    *    IGHT,Y_UP,Y_DOWN,LETTERSIZE四个变量决定。
    *返回值：无返回值
    *参数：QPainter * pp 为传入的屏幕缓冲的画笔，一切绘图均在缓冲中进
    *     行，绘图采用双缓冲机制。
    *备注：传入的QPainter不能为空。
    ********************************************************/
    void print(QPainter * pp){
        Linelink * BUFFER=CURLINE;
        Filelink *FILELINK=CURLINE->c;
        bool chosen=first_chosen();
        QFont font;
        font.setPixelSize(LETTERSIZE-3);
        pp->setFont(font);
        int X_POS=X_LEFT,Y_POS=Y_UP+LETTERSIZE;
        if(CURLINE->c&&CURLINE->c->last&&CURLINE->c->last==POINTER_T->headF&&POINTER_T->note==POINTER_T->headF){
            if(note_show)pp->drawText(QPoint(X_POS-LETTERSIZE/2+2,Y_POS),QString("|"));
        }
        for(;X_POS<=X_RIGHT-LETTERSIZE && Y_POS<=Y_DOWN &&FILELINK &&FILELINK->next;){
            char ch=FILELINK->text[0];
            if(FILELINK==POINTER_T->res.left){
                chosen=TRUE;
            }
            if(chosen){
                pp->setPen(QColor(Qt::black));
                if(FILELINK&&FILELINK->text&&FILELINK->text[0]<0)
                    pp->fillRect(X_POS,Y_POS-LETTERSIZE+4,LETTERSIZE,LETTERSIZE,QColor(Qt::black));
                else if(FILELINK&&FILELINK->text&&FILELINK->text[0]=='\t')
                    pp->fillRect(X_POS,Y_POS-LETTERSIZE+4,LETTERSIZE*2,LETTERSIZE,QColor(Qt::black));
                else if(FILELINK&&FILELINK->text&&FILELINK->text[0]>0)
                    pp->fillRect(X_POS,Y_POS-LETTERSIZE+4,LETTERSIZE/2,LETTERSIZE,QColor(Qt::black));
                pp->setPen(QColor(Qt::white));
            }else
                pp->setPen(QColor(Qt::black));
            if(FILELINK->text[0]=='\n')
                pp->drawPixmap(X_POS,Y_POS-enter.height(),enter);
            else
                pp->drawText(QPoint(X_POS,Y_POS),QString(QString::fromLocal8Bit(FILELINK->text)));
            pp->setPen(QColor(Qt::black));
            if(FILELINK->last==POINTER_T->note&&POINTER_T->headF&&FILELINK->last!=POINTER_T->headF){
                if(note_show)pp->drawText(QPoint(X_POS-LETTERSIZE/4,Y_POS),QString("|"));
            }
            if(FILELINK->next&&FILELINK->next->next==NULL&&POINTER_T->note==FILELINK&&note_show){
                if(FILELINK->text[0]!='\n')
                    if(FILELINK->text[0]<0)
                        pp->drawText(QPoint(X_POS+LETTERSIZE/2+2,Y_POS),QString("|"));
                    else if(FILELINK->text[0]=='\t')
                        pp->drawText(QPoint(X_POS+2*LETTERSIZE-4,Y_POS),QString("|"));
                    else
                        pp->drawText(QPoint(X_POS+LETTERSIZE/2-4,Y_POS),QString("|"));
                else
                        pp->drawText(QPoint(X_LEFT,Y_POS+LETTERSIZE),QString("|"));
            }
            if(FILELINK==POINTER_T->res.right){
                chosen=FALSE;
            }
            if(FILELINK&&FILELINK->text&&FILELINK->text[0]<0)
                X_POS+=LETTERSIZE;
            else if(FILELINK&&FILELINK->text&&FILELINK->text[0]=='\t')
                X_POS+=2*LETTERSIZE;
            else
                X_POS+=LETTERSIZE/2;
            if(BUFFER&& BUFFER->next&&FILELINK->next==BUFFER->next->c){
                X_POS=X_LEFT;
                Y_POS+=LETTERSIZE;
                if(BUFFER&&BUFFER->next){
                    BUFFER=BUFFER->next;
                    FILELINK=BUFFER->c;
                }

            }
            else if(FILELINK->next)
                FILELINK=FILELINK->next;
            else
                break;
        }
    }
    boolean first_chosen(){
        bool chosen=false;
        for(Filelink *TEMP=POINTER_T->headF;TEMP&&TEMP!=CURLINE->c;TEMP=TEMP->next){
            if(TEMP==POINTER_T->res.left)
                chosen=true;
            if(TEMP==POINTER_T->res.right)
                chosen=false;
        }
        return chosen;
    }
    boolean buffer_full(){
        return POINTER_T->buffer_exist();
    }
    /*******************************************************
    *函数名：to_next_line()
    *功能:将当前行指针指向下一行，若无下一行，则不进行操作
    *返回值：无返回值
    *参数：无参数
    *备注：无
    ********************************************************/
    void to_next_line(){
        note_down();
    }
    /*******************************************************
    *函数名：to_last_line()
    *功能:将当前行指针指向上一行，若无上一行，则不进行操作
    *返回值：无返回值
    *参数：无参数
    *备注：无
    ********************************************************/
    void to_last_line(){
        note_up();
    }
    /*******************************************************
    *函数名：note_left()
    *功能:当前光标向左移动一个单位
    *返回值：无返回值
    *参数：无参数
    *备注：无
    ********************************************************/
    void note_left(){
        POINTER_T->note_move_left();
    }
    /*******************************************************
    *函数名：note_right()
    *功能:当前光标向右移动一个单位
    *返回值：无返回值
    *参数：无参数
    *备注：无
    ********************************************************/
    void note_right(){
        POINTER_T->note_move_right();
    }
    /*******************************************************
    *函数名：note_up()
    *功能:当前光标向上移动一个单位
    *返回值：无返回值
    *参数：无参数
    *备注：无
    ********************************************************/
    void note_up(){
        if(NOTELINE&&NOTELINE->last){
             Linelink * BUFFER=NOTELINE->last;
             POINTER_T->note=BUFFER->c;
             for(int i=0;i<NOTE_X&&POINTER_T->note->next&&POINTER_T->note!=BUFFER->next->c&&POINTER_T->note->next!=BUFFER->next->c&&POINTER_T->note->next->next!=BUFFER->next->c;){
                POINTER_T->note=POINTER_T->note->next;
                if(POINTER_T->note&&POINTER_T->note->text&&POINTER_T->note->text[0]<0)
                    i+=LETTERSIZE;
                else if(POINTER_T->note&&POINTER_T->note->text&&POINTER_T->note->text[0]=='\t')
                    i+=2*LETTERSIZE;
                else
                    i+=LETTERSIZE/2;
             }
             if(NOTELINE==CURLINE&&CURLINE->last)
                 CURLINE=CURLINE->last;
             if(NOTELINE->c==POINTER_T->note&&POINTER_T->note->text[0]=='\n')
                POINTER_T->note=POINTER_T->note->last;
             POINTER_T->res.left=NULL;
             POINTER_T->res.right=NULL;
        }
    }
    /*******************************************************
    *函数名：note_down()
    *功能:当前光标向下移动一个单位
    *返回值：无返回值
    *参数：无参数
    *备注：无
    ********************************************************/
    void note_down(){        
        if(NOTELINE&&NOTELINE->next){
             Linelink * BUFFER=NOTELINE->next;
             POINTER_T->note=BUFFER->c;
             for(int i=0;i<NOTE_X&&POINTER_T->note->next&&(!BUFFER->next||POINTER_T->note!=BUFFER->next->c&&POINTER_T->note->next!=BUFFER->next->c&&POINTER_T->note->next->next!=BUFFER->next->c);){
                POINTER_T->note=POINTER_T->note->next;
                if(POINTER_T->note&&POINTER_T->note->text&&POINTER_T->note->text[0]<0)
                    i+=LETTERSIZE;
                else if(POINTER_T->note&&POINTER_T->note->text&&POINTER_T->note->text[0]=='\t')
                    i+=2*LETTERSIZE;
                else
                    i+=LETTERSIZE/2;
             }
             if(DOWNLINE&&DOWNLINE->next)
             if(NOTELINE==DOWNLINE->last&&CURLINE&&CURLINE->next)
                 CURLINE=CURLINE->next;
             POINTER_T->res.left=NULL;
             POINTER_T->res.right=NULL;
        }
    }
    /*******************************************************
    *函数名：rool_up()
    *功能:整页向上滑动一行
    *返回值：无返回值
    *参数：无参数
    *备注：用于响应鼠标滚轮滚动事件
    ********************************************************/
    void rool_up(){
        if(CURLINE&&CURLINE->last)
            CURLINE=CURLINE->last;
    }
    /*******************************************************
    *函数名：rool_down()
    *功能:整页向下滑动一行
    *返回值：无返回值
    *参数：无参数
    *备注：用于响应鼠标滚轮滚动事件
    ********************************************************/
    void rool_down(){
        if(CURLINE&&CURLINE->next)
            CURLINE=CURLINE->next;
    }
    /*******************************************************
    *函数名：insert(QString q)
    *功能:整页向下滑动一行
    *返回值：无返回值
    *参数：QString q为应该插入的字符（串）
    *备注：该函数用于衔接windowmanager与tmanager之间的插入请求及进行
    *     从QString到char*的类型转换
    ********************************************************/
    void insert(char *str,int job=0,int special=1){
        printf("insert--%s--",str);
        for(int i=0;str[i]!=0;){
            char* TEMPSREING=(char*)malloc(3*sizeof(char));
            if(str[i]<0){
                TEMPSREING[0]=str[i];
                TEMPSREING[1]=str[i+1];
                TEMPSREING[2]=0;
                i+=2;
            }else{
                TEMPSREING[0]=str[i];
                TEMPSREING[1]=0;
                TEMPSREING[2]=0;
                i+=1;
            }
            POINTER_T->Insert(TEMPSREING,job);
        }
        //for(int i=0;i<128;i++)
        //    str[i]=0;
        if(special)refresh_linelinks();
        JUST_INSERTED=TRUE;
    }
    void insert_filelink(Filelink * filelink){
        Filelink * TEMPLINK=filelink;
        if(TEMPLINK->next){
        insert(TEMPLINK->text,1);
        TEMPLINK=TEMPLINK->next;
        }
        for(;TEMPLINK->next;TEMPLINK=TEMPLINK->next){
            insert(TEMPLINK->text,2);
        }
        POINTER_T->note=NULL;
    }
    /*******************************************************
    *函数名：Backspace()
    *功能:删除光标之前的一个字符
    *返回值：无返回值
    *参数：无
    *备注：该函数用于衔接windowmanager与tmanager之间的回格请求
    ********************************************************/
    void Backspace(){
        if(NOTE_X<=0&&NOTE_Y<=Y_UP)
            rool_up();
        POINTER_T->Delete();
        refresh_linelinks();
        link_check();
        JUST_DELETED=TRUE;
    }
    /*******************************************************
    *函数名：Delete()
    *功能:删除光标之后的一个字符
    *返回值：无返回值
    *参数：无
    *备注：该函数用于衔接windowmanager与tmanager之间的向后删除请求
    ********************************************************/
    void Delete(){
        POINTER_T->Cancel();
        refresh_linelinks();
        link_check();
    }
    void copy(){
        POINTER_T->copy();
    }
    void cut(){
        POINTER_T->cut();
        refresh_linelinks();
        link_check();
    }
    void paste(){
        char *str;
        QByteArray ba = QApplication::clipboard()->text().toLocal8Bit();
        bool first_ptr=TRUE;
        str=ba.data();
        for(int i=0;str[i]!=0;){
            char* TEMPSREING=(char*)malloc(3*sizeof(char));
            if(str[i]<0){
                TEMPSREING[0]=str[i];
                TEMPSREING[1]=str[i+1];
                TEMPSREING[2]=0;
                i+=2;
            }else{
                TEMPSREING[0]=str[i];
                TEMPSREING[1]=0;
                TEMPSREING[2]=0;
                i+=1;
            }
            if(first_ptr)
                POINTER_T->Insert(TEMPSREING,1),first_ptr=FALSE;
            else
                POINTER_T->Insert(TEMPSREING,2);
        }
        refresh_linelinks();
    }
    void save(){
        POINTER_T->save(name);
    }
    void save(char* string){
        POINTER_T->save(string);
    }
    void link_check(){
         Linelink * TEMP=headline;
         for(;TEMP;TEMP=TEMP->next)
             if(TEMP==CURLINE)
                 return;
         Linelink* TEMPLINE=headline;
         Filelink* TEMPLINK=headline->c;
         if(POINTER_T->note)
             for(;TEMPLINE&&TEMPLINK;TEMPLINK=TEMPLINK->next){
                 if(TEMPLINK==POINTER_T->note)
                     CURLINE=TEMPLINE;
                 if(TEMPLINK&&TEMPLINE->next&&TEMPLINK->next==TEMPLINE->next->c)
                     TEMPLINE=TEMPLINE->next;
             }
         else
             CURLINE=headline;
    }
    /*******************************************************
    *函数名：seek_note(int x,int y)
    *功能:根据鼠标在屏幕上的单击位置重新确定光标位置
    *返回值：无返回值
    *参数：x,y分别为鼠标的x和y坐标
    *备注：重新确定的光标note将停留在离单击点尽可能近的地方
    ********************************************************/
    void seek_note(int x,int y,int kind){
        if(kind==4||kind==5||X_LEFT<x && x<X_RIGHT && y>Y_UP&& y<Y_DOWN){
            Linelink * TEMPLINE=CURLINE;
            Filelink * TEMPLINK;
            if(kind==4)
                x=X_RIGHT-1,y=Y_DOWN-1,kind=3;
            if(kind==5)
                x=X_LEFT+1,y=Y_UP-1,kind=3;
            int line_add=(y-Y_UP)/LETTERSIZE,link_add=(x-X_LEFT-LETTERSIZE/3);
            for(int i=0;i<line_add&&(TEMPLINE&&TEMPLINE->next);i++){
                TEMPLINE=TEMPLINE->next;
            }
            NOTELINE=TEMPLINE;
            if(TEMPLINE->c){
                TEMPLINK=TEMPLINE->c;
                if(TEMPLINK->last)TEMPLINK=TEMPLINK->last;
                for(int i=1;i<=link_add&&(TEMPLINK&&TEMPLINK->next&&TEMPLINK->next->next&&(!(TEMPLINE)||!(TEMPLINE->next)||!(TEMPLINE->next->next)||(TEMPLINE->next->c!=TEMPLINK&&TEMPLINE->next->c!=TEMPLINK->next&&TEMPLINE->next->c!=TEMPLINK->next->next)));){
                    TEMPLINK=TEMPLINK->next;
                    if(TEMPLINK&&TEMPLINK->text&&TEMPLINK->text[0]<0)
                        i+=LETTERSIZE;
                    else if(TEMPLINK&&TEMPLINK->text&&TEMPLINK->text[0]=='\t')
                        i+=2*LETTERSIZE;
                    else
                        i+=LETTERSIZE/2;
                }
            }
            if(kind==1)
                POINTER_T->note=TEMPLINK,res_note=POINTER_T->note->next;
            else if(kind==2)
                POINTER_T->res.left=TEMPLINK->next;
            else if(kind==3)
                if(!before_res(TEMPLINK)){
                        POINTER_T->res.right=TEMPLINK;
                        POINTER_T->res.left=res_note;
                }else{
                        POINTER_T->res.right=res_note->last;
                        POINTER_T->res.left=TEMPLINK->next;
                        if(POINTER_T->res.right->next==POINTER_T->res.left)
                             POINTER_T->res.left=NULL;
                }
        }
    }
    boolean before_res(Filelink * T){
        if(T)
            for(;T;T=T->next)
                if(res_note->last&&T==res_note->last){
                    return TRUE;
                }
        return FALSE;
    }
    void clear_note(){
        POINTER_T->note=NULL;
    }
    void clear_res(){
        POINTER_T->res.left=NULL;
        POINTER_T->res.right=NULL;
    }
    void clear(){
        POINTER_T=new Tmanager("TEMP.txt");
    }
    bool res_check(){
        return POINTER_T->res_check();
    }
    void page_up(){
        int line_num=(Y_DOWN-Y_UP)/LETTERSIZE;
        for(int i=0;i<line_num;i++){
            if(CURLINE->last)
                CURLINE=CURLINE->last;
        }
    }
    void page_down(){
        int line_num=(Y_DOWN-Y_UP)/LETTERSIZE;
        for(int i=0;i<line_num;i++){
            if(CURLINE->next)
                CURLINE=CURLINE->next;
        }
    }
    void home_pressed(){
        printf("in");
        if(NOTELINE){
            if(NOTELINE->c&& NOTELINE->c->last)
                POINTER_T->note=NOTELINE->c->last;
            else
                POINTER_T->note=NOTELINE->c;
        }
    }
    void mouse_shift_click(int x,int y){
        if(POINTER_T->note){
            POINTER_T->res.left=POINTER_T->note&&POINTER_T->note->next?POINTER_T->note->next:POINTER_T->note;
            seek_note(x,y,3);
            POINTER_T->note=NULL;
            res_check();
        }else if(POINTER_T->res.left){
            seek_note(x,y,3);
            POINTER_T->note=NULL;
            res_check();
        }
    }
    void store(){
        if(CURLINE&&CURLINE->c){
            int i=0;
            for(Filelink*  TEMP=POINTER_T->headF;TEMP&&TEMP!=CURLINE->c;TEMP=TEMP->next)
                i++;
            store(name,i);
            qDebug()<<i;
        }
    }
    void read(){
        int j=read(name);
        if(j>=0){
            qDebug()<<j;
            Filelink* TEMP=POINTER_T->headF;
            for(int i=0;i<j;i++)
                if(TEMP)TEMP=TEMP->next;
            Linelink* TEMPLINE=headline;
            Filelink* TEMPLINK=headline->c;
            for(;TEMPLINE&&TEMPLINK;TEMPLINK=TEMPLINK->next){
                if(TEMPLINK==TEMP){
                    CURLINE=TEMPLINE;
                    return;
                }
                if(TEMPLINK&&TEMPLINE->next&&TEMPLINK->next==TEMPLINE->next->c)
                    TEMPLINE=TEMPLINE->next;
            }
        }
    }
    void store(char* name, int data)
    {
        int num = 0;
        FILE* fPtr = NULL;
        Shuqian p;

        while(*name){
            num = 2 * num + (int)*name;
            name++;
        }

        p.n = num;
        p.d = data;
        if((fPtr = fopen("shuqian.txt", "a"))!=NULL)
                fwrite(&p, sizeof(p), 1, fPtr);
        fclose(fPtr);

    }
    int read(char* name)
    {
        FILE* fPtr = NULL;
        int num = 0, i, n;
        Shuqian p;

        while(*name !='\0'){
            num = 2 * num + (int)*name;
            name++;
        }
        if((fPtr = fopen("shuqian.txt", "r"))!= NULL){
            fseek(fPtr, -sizeof(p), SEEK_END);
            if(fread(&p, sizeof(p), 1, fPtr)){
                for(i = 1; p.n != num ; i++){
                    n = fseek(fPtr, -(sizeof(p) * i), SEEK_END);
                    fread(&p, sizeof(p), 1, fPtr);
                    if(n == -1)
                        return -1;
                }
                if(p.n == num)
                    return p.d;
            }

        }
    }
private:
    /*******************************************************
    *函数名：create_pagelinks()
    *功能:在初始化时进行各行的划分
    *返回值：无返回值
    *参数：无参数
    *备注：该函数在初始化时必须调用，进行行的初始化
    ********************************************************/
    void create_linelinks(){
        Linelink * cur=NULL,* next=NULL;
        Filelink* FILELINK=POINTER_T->headF->next;
        int X_POS=X_LEFT,Y_POS=Y_UP+LETTERSIZE;
        headline=(Linelink*)malloc(sizeof(Linelink));
        headline->c=FILELINK;
        headline->last=NULL;
        headline->next=NULL;
        cur=headline;
        while(FILELINK){
            while(X_POS<=X_RIGHT-LETTERSIZE ){
                char ch;
                if(FILELINK)
                    ch=FILELINK->text[0];
                if(FILELINK&&FILELINK->text&&FILELINK->text[0]<0)
                    X_POS+=LETTERSIZE;
                else if(FILELINK&&FILELINK->text&&FILELINK->text[0]=='\t')
                    X_POS+=2*LETTERSIZE;
                else
                    X_POS+=LETTERSIZE/2;
                if(ch=='\n')
                    X_POS+=(X_RIGHT-X_LEFT);
                /*if(X_POS>=X_RIGHT){
                    X_POS=X_LEFT;
                    Y_POS+=LETTERSIZE;
                }

                if(ch=='\n'){
                    X_POS=X_LEFT;
                    Y_POS+=LETTERSIZE;
                }*/
                if(FILELINK)
                    FILELINK=FILELINK->next;
                else
                    break;
            }
            X_POS=X_LEFT;
            Y_POS=Y_UP;
            if(FILELINK){
                next=(Linelink*)malloc(sizeof(Linelink));
                next->last=cur;
                next->next=NULL;
                next->c=FILELINK;
                cur->next=next;
                cur=next;
                next=NULL;
            }
        }
        CURLINE=headline;
    }
    /*******************************************************
    *函数名：refresh_linelinks()
    *功能:重新进行各行的划分，尽可能少的多申请内存
    *返回值：无返回值
    *参数：无参数
    *备注：在文本内容发生变化时（如插入，删除后）调用此函数，可以重新划分
    *     文本中的各行
    ********************************************************/
    void refresh_linelinks(){
        Linelink * cur=NULL,* next=NULL;
        Filelink* FILELINK=POINTER_T->headF->next;
        int X_POS=X_LEFT,Y_POS=Y_UP+LETTERSIZE;
        headline->c=POINTER_T->headF->next;
        cur=headline;
        while(FILELINK){
            while(X_POS<=X_RIGHT-LETTERSIZE ){
                char ch;
                if(FILELINK)
                    ch=FILELINK->text[0];
                if(FILELINK&&FILELINK->text&&FILELINK->text[0]<0)
                    X_POS+=LETTERSIZE;
                else if(FILELINK&&FILELINK->text&&FILELINK->text[0]=='\t')
                    X_POS+=2*LETTERSIZE;
                else
                    X_POS+=LETTERSIZE/2;
                if(ch=='\n')
                    X_POS+=(X_RIGHT-X_LEFT);
                if(FILELINK)
                    FILELINK=FILELINK->next;
                else
                    break;
            }
            X_POS=X_LEFT;
            Y_POS=Y_UP;
            if(FILELINK&&!(cur->next)){
                next=(Linelink*)malloc(sizeof(Linelink));
                next->last=cur;
                next->next=NULL;
                next->c=FILELINK;
                cur->next=next;
                cur=next;
                next=NULL;
            }else if(FILELINK){
                cur=cur->next;
                cur->c=FILELINK;
            }
        }
        cur->next=NULL;
    }
};
#endif
